﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EDiamondModel;

public partial class Admin_HinhThucVanChuyen : System.Web.UI.Page
{
    EDiamondEntities1 db = new EDiamondEntities1();
    void LoadHinhThucVanChuyen()
    {
        gvHinhThucThanhToan.DataKeyNames = new string[] { "Id" };
        var query = db.HinhThucVanChuyens;
        gvHinhThucThanhToan.DataSource = query;
        gvHinhThucThanhToan.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadHinhThucVanChuyen();
        }
    }
    protected void gvHinhThucThanhToan_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvHinhThucThanhToan.EditIndex = -1;
        LoadHinhThucVanChuyen();
    }
    protected void gvHinhThucThanhToan_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if ((e.Row.RowType == DataControlRowType.DataRow) && (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate))
        {
            LinkButton Imbt = (LinkButton)e.Row.Cells[3].Controls[2];
            Imbt.OnClientClick = "if (!confirm('Bạn có đồng ý hủy không?')) return false;";
        }
    }
    protected void gvHinhThucThanhToan_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int khoa = Int32.Parse(gvHinhThucThanhToan.DataKeys[e.RowIndex].Value.ToString());
        try
        {

            HinhThucVanChuyen entity = db.HinhThucVanChuyens.Include("DatHangs").SingleOrDefault(p => p.Id == khoa);

            db.HinhThucVanChuyens.DeleteObject(entity);
            db.SaveChanges();
            lbThongBao.Text = "Hủy thành công";
            LoadHinhThucVanChuyen();
        }
        catch (Exception ex)
        {
            lbThongBao.Text = "Hủy không thành công";
        }
    }
    protected void gvHinhThucThanhToan_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvHinhThucThanhToan.EditIndex = e.NewEditIndex;
        LoadHinhThucVanChuyen();
    }
    protected void gvHinhThucThanhToan_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string ten = ((TextBox)gvHinhThucThanhToan.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string ma = ((TextBox)gvHinhThucThanhToan.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        //string kh = ((TextBox)gvHinhThucThanhToan.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
        if (!string.IsNullOrEmpty(ten))
        {
            try
            {
                Int16 khoa = System.Convert.ToInt16(gvHinhThucThanhToan.DataKeys[e.RowIndex].Value);
                HinhThucVanChuyen entiry = db.HinhThucVanChuyens.FirstOrDefault(p => p.Id == khoa);
                entiry.Ten = ten;
                entiry.Id = Int32.Parse(ma);
                entiry.KyHieu = Thuvien.BoDauTiengViet(ten);

                db.SaveChanges();

                gvHinhThucThanhToan.EditIndex = -1;
                lbThongBao.Text = "Cập nhật thành công!";
                LoadHinhThucVanChuyen();
            }
            catch (Exception ex)
            {
                lbThongBao.Text = "Cập nhật không thành công!" + ex.Message;
            }
        }
        else
            e.Cancel = true;
    }
}